/**
 *帧频触发函数集
 * 使用addHandle()来统一做帧频函数处理，有利于保持渲染同步 
 */
package utils.time
{
	import flash.display.MovieClip;
	import flash.events.Event;

	public class EnterFrameHandle
	{
		private static var _instance:EnterFrameHandle;
		private var _mc:MovieClip;
		private var _hands:Vector.<Function>;
		
		public function EnterFrameHandle()
		{
		}
		
		private static function get instance():EnterFrameHandle
		{
			if(_instance == null)
			{
				_instance = new EnterFrameHandle();
			}
			
			return _instance;
		}
		
		/**
		 *添加帧频处理函数 
		 * @param handle
		 * 
		 */		
		public static function addHandle(handle:Function):void
		{
			if(instance._mc == null)
			{
				instance._mc = new MovieClip();
				instance._mc.addEventListener(Event.ENTER_FRAME,instance.onEnterFrame);
			}
			if(instance._hands == null)
			{
				instance._hands = new Vector.<Function>();
			}
			
			if(instance._hands.indexOf(handle) != -1)
				return ;
			instance._hands.push(handle);
		}
		
		/**
		 *移除 帧频处理函数 
		 * @param handle
		 * 
		 */		
		public static function removeHandle(handle:Function):void
		{
			var index:int = instance._hands.indexOf(handle);
			if(index != -1)
				instance._hands.splice(index,1);
		}
		
		private function onEnterFrame(evt:Event):void
		{
			var handle:Function;
			for each(handle in _hands)
			{
				handle();
			}
		}
	}
}